package com.trendata.service;

import java.sql.Timestamp;

import org.springframework.stereotype.Service;

import com.trendata.util.CacheUtil;
import com.trendata.util.WeChatUtil;

/**
 * 缓存
 * 
 * @author: HuangXiuXiu Email:243559392@qq.com
 */
@Service("cacheService")
public class CacheService {

	/**
	 * 获取微信accessToken
	 * 
	 * @param timestamp
	 * @return
	 */
	public String getAccessToken(Timestamp timestamp) {
		Object object = CacheUtil.get(timestamp.toString());
		if (object == null) {
			String accessToken = WeChatUtil.getAccessToken();
			CacheUtil.put("sstoken" + timestamp.toString(), 3600, accessToken);
			return accessToken;
		}
		return (String) object;
	}

	public void removeAccessToken(Timestamp timestamp) {
		CacheUtil.delete("sstoken" + timestamp.toString());
	}

	/**
	 * 获取微信jsTicket
	 * 
	 * @param timestamp
	 * @return
	 */
	public String getJSTicket(Timestamp timestamp) {
		Object object = CacheUtil.get("ssticket" + timestamp.toString());
		if (object == null) {
			String accessToken = this.getAccessToken(timestamp);
			String jsTicket = WeChatUtil.getJSTicket(accessToken);
			CacheUtil.put("ssticket" + timestamp.toString(), 3600, jsTicket);
			return jsTicket;
		}
		return (String) object;
	}

	/**
	 * 删除jsticket
	 * 
	 * @param timestamp
	 */
	public void removeJSTicket(Timestamp timestamp) {
		CacheUtil.delete("ssticket" + timestamp.toString());
	}
}
